﻿' ----------------------------------------------------------------------------------
' Microsoft Developer & Platform Evangelism
' 
' Copyright (c) Microsoft Corporation. All rights reserved.
' 
' THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
' OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ----------------------------------------------------------------------------------
' The example companies, organizations, products, domain names,
' e-mail addresses, logos, people, places, and events depicted
' herein are fictitious.  No association with any real company,
' organization, product, domain name, email address, logo, person,
' places, or events is intended or should be inferred.
' ----------------------------------------------------------------------------------

Imports System.Globalization
Imports System.ComponentModel.DataAnnotations
Imports System.ComponentModel
Imports System

Namespace Infrastructure

    <AttributeUsage(AttributeTargets.Class, AllowMultiple:=True, Inherited:=True)>
    Public NotInheritable Class PropertiesMustMatchAttribute
        Inherits ValidationAttribute
        Private Const DefaultErrorMessage As String = "'{0}' and '{1}' do not match."

        Private ReadOnly typeIdValue As New Object()

        Public Sub New(ByVal originalProperty As String, ByVal confirmProperty As String)
            MyBase.New(DefaultErrorMessage)
            Me.OriginalProperty = originalProperty
            Me.ConfirmProperty = confirmProperty
        End Sub

        Private privateConfirmProperty As String
        Public Property ConfirmProperty() As String
            Get
                Return privateConfirmProperty
            End Get
            Private Set(ByVal value As String)
                privateConfirmProperty = value
            End Set
        End Property

        Private privateOriginalProperty As String
        Public Property OriginalProperty() As String
            Get
                Return privateOriginalProperty
            End Get
            Private Set(ByVal value As String)
                privateOriginalProperty = value
            End Set
        End Property

        Public Overrides ReadOnly Property TypeId() As Object
            Get
                Return Me.typeIdValue
            End Get
        End Property

        Public Overrides Function FormatErrorMessage(ByVal name As String) As String
            Return String.Format(CultureInfo.CurrentUICulture, Me.ErrorMessageString, Me.OriginalProperty, Me.ConfirmProperty)
        End Function

        Public Overloads Overrides Function IsValid(ByVal value As Object) As Boolean
            Dim properties = TypeDescriptor.GetProperties(value)
            Dim originalValue As Object = properties.Find(Me.OriginalProperty, True).GetValue(value)
            Dim confirmValue As Object = properties.Find(Me.ConfirmProperty, True).GetValue(value)

            Return Object.Equals(originalValue, confirmValue)
        End Function
    End Class
End Namespace